<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<style>
	body {margin: 0; padding: 10px; background-color: #ffffff}
	h1 {margin: 5px 0 0 0; font-size: 18px; font-weight: normal; text-align: center}
	header {margin: -24px 0 5px 0; line-height: 24px}
	button {font: 12px sans-serif; cursor: pointer}
	p {margin: 5px 0 5px 0}
	a {color: #0366d6}
	#hl {position: absolute; display: none; overflow: hidden; white-space: nowrap; pointer-events: none; background-color: #ffffe0; outline: 1px solid #ffc000; height: 15px}
	#hl span {padding: 0 3px 0 3px}
	#status {overflow: hidden; white-space: nowrap}
	#match {overflow: hidden; white-space: nowrap; display: none; float: right; text-align: right}
	#reset {cursor: pointer}
	#canvas {width: 100%; height: 640px}
</style>
</head>
<body style='font: 12px Verdana, sans-serif'>
<h1>CPU profile</h1>
<header style='text-align: left'><button id='reverse' title='Reverse'>&#x1f53b;</button>&nbsp;&nbsp;<button id='search' title='Search'>&#x1f50d;</button></header>
<header style='text-align: right'>Produced by <a href='https://github.com/jvm-profiling-tools/async-profiler'>async-profiler</a></header>
<canvas id='canvas'></canvas>
<div id='hl'><span></span></div>
<p id='match'>Matched: <span id='matchval'></span> <span id='reset' title='Clear'>&#x274c;</span></p>
<p id='status'>&nbsp;</p>
<script>
	// Copyright 2020 Andrei Pangin
	// Licensed under the Apache License, Version 2.0.
	'use strict';
	var root, rootLevel, px, pattern;
	var reverse = false;
	const levels = Array(40);
	for (let h = 0; h < levels.length; h++) {
		levels[h] = [];
	}

	const canvas = document.getElementById('canvas');
	const c = canvas.getContext('2d');
	const hl = document.getElementById('hl');
	const status = document.getElementById('status');

	const canvasWidth = canvas.offsetWidth;
	const canvasHeight = canvas.offsetHeight;
	canvas.style.width = canvasWidth + 'px';
	canvas.width = canvasWidth * (devicePixelRatio || 1);
	canvas.height = canvasHeight * (devicePixelRatio || 1);
	if (devicePixelRatio) c.scale(devicePixelRatio, devicePixelRatio);
	c.font = document.body.style.font;

	const palette = [
		[0xb2e1b2, 20, 20, 20],
		[0x50e150, 30, 30, 30],
		[0x50cccc, 30, 30, 30],
		[0xe15a5a, 30, 40, 40],
		[0xc8c83c, 30, 30, 10],
		[0xe17d00, 30, 30,  0],
		[0xcce880, 20, 20, 20],
	];

	function getColor(p) {
		const v = Math.random();
		return '#' + (p[0] + ((p[1] * v) << 16 | (p[2] * v) << 8 | (p[3] * v))).toString(16);
	}

	function f(level, left, width, type, title, inln, c1, int) {
		levels[level].push({left: left, width: width, color: getColor(palette[type]), title: title,
			details: (int ? ', int=' + int : '') + (c1 ? ', c1=' + c1 : '') + (inln ? ', inln=' + inln : '')
		});
	}

	function samples(n) {
		return n === 1 ? '1 sample' : n.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',') + ' samples';
	}

	function pct(a, b) {
		return a >= b ? '100' : (100 * a / b).toFixed(2);
	}

	function findFrame(frames, x) {
		let left = 0;
		let right = frames.length - 1;

		while (left <= right) {
			const mid = (left + right) >>> 1;
			const f = frames[mid];

			if (f.left > x) {
				right = mid - 1;
			} else if (f.left + f.width <= x) {
				left = mid + 1;
			} else {
				return f;
			}
		}

		if (frames[left] && (frames[left].left - x) * px < 0.5) return frames[left];
		if (frames[right] && (x - (frames[right].left + frames[right].width)) * px < 0.5) return frames[right];

		return null;
	}

	function search(r) {
		if (r === true && (r = prompt('Enter regexp to search:', '')) === null) {
			return;
		}

		pattern = r ? RegExp(r) : undefined;
		const matched = render(root, rootLevel);
		document.getElementById('matchval').textContent = pct(matched, root.width) + '%';
		document.getElementById('match').style.display = r ? 'inherit' : 'none';
	}

	function render(newRoot, newLevel) {
		if (root) {
			c.fillStyle = '#ffffff';
			c.fillRect(0, 0, canvasWidth, canvasHeight);
		}

		root = newRoot || levels[0][0];
		rootLevel = newLevel || 0;
		px = canvasWidth / root.width;

		const x0 = root.left;
		const x1 = x0 + root.width;
		const marked = [];

		function mark(f) {
			return marked[f.left] >= f.width || (marked[f.left] = f.width);
		}

		function totalMarked() {
			let total = 0;
			let left = 0;
			Object.keys(marked).sort(function(a, b) { return a - b; }).forEach(function(x) {
				if (+x >= left) {
					total += marked[x];
					left = +x + marked[x];
				}
			});
			return total;
		}

		function drawFrame(f, y, alpha) {
			if (f.left < x1 && f.left + f.width > x0) {
				c.fillStyle = pattern && f.title.match(pattern) && mark(f) ? '#ee00ee' : f.color;
				c.fillRect((f.left - x0) * px, y, f.width * px, 15);

				if (f.width * px >= 21) {
					const chars = Math.floor(f.width * px / 7);
					const title = f.title.length <= chars ? f.title : f.title.substring(0, chars - 2) + '..';
					c.fillStyle = '#000000';
					c.fillText(title, Math.max(f.left - x0, 0) * px + 3, y + 12, f.width * px - 6);
				}

				if (alpha) {
					c.fillStyle = 'rgba(255, 255, 255, 0.5)';
					c.fillRect((f.left - x0) * px, y, f.width * px, 15);
				}
			}
		}

		for (let h = 0; h < levels.length; h++) {
			const y = reverse ? h * 16 : canvasHeight - (h + 1) * 16;
			const frames = levels[h];
			for (let i = 0; i < frames.length; i++) {
				drawFrame(frames[i], y, h < rootLevel);
			}
		}

		return totalMarked();
	}

	canvas.onmousemove = function() {
		const h = Math.floor((reverse ? event.offsetY : (canvasHeight - event.offsetY)) / 16);
		if (h >= 0 && h < levels.length) {
			const f = findFrame(levels[h], event.offsetX / px + root.left);
			if (f) {
				if (f != root) getSelection().removeAllRanges();
				hl.style.left = (Math.max(f.left - root.left, 0) * px + canvas.offsetLeft) + 'px';
				hl.style.width = (Math.min(f.width, root.width) * px) + 'px';
				hl.style.top = ((reverse ? h * 16 : canvasHeight - (h + 1) * 16) + canvas.offsetTop) + 'px';
				hl.firstChild.textContent = f.title;
				hl.style.display = 'block';
				canvas.title = f.title + '\n(' + samples(f.width) + f.details + ', ' + pct(f.width, levels[0][0].width) + '%)';
				canvas.style.cursor = 'pointer';
				canvas.onclick = function() {
					if (f != root) {
						render(f, h);
						canvas.onmousemove();
					}
				};
				status.textContent = 'Function: ' + canvas.title;
				return;
			}
		}
		canvas.onmouseout();
	}

	canvas.onmouseout = function() {
		hl.style.display = 'none';
		status.textContent = '\xa0';
		canvas.title = '';
		canvas.style.cursor = '';
		canvas.onclick = '';
	}

	canvas.ondblclick = function() {
		getSelection().selectAllChildren(hl);
	}

	document.getElementById('reverse').onclick = function() {
		reverse = !reverse;
		render();
	}

	document.getElementById('search').onclick = function() {
		search(true);
	}

	document.getElementById('reset').onclick = function() {
		search(false);
	}

	window.onkeydown = function() {
		if (event.ctrlKey && event.keyCode === 70) {
			event.preventDefault();
			search(true);
		} else if (event.keyCode === 27) {
			search(false);
		}
	}

f(0,0,8,3,'all')
f(1,0,5,1,'java/lang/Thread.run')
f(2,0,4,1,'com/alibaba/arthas/deps/io/netty/util/concurrent/FastThreadLocalRunnable.run')
f(3,0,4,1,'com/alibaba/arthas/deps/io/netty/util/internal/ThreadExecutorMap$2.run')
f(4,0,4,1,'com/alibaba/arthas/deps/io/netty/util/concurrent/SingleThreadEventExecutor$4.run')
f(5,0,4,1,'com/alibaba/arthas/deps/io/netty/channel/nio/NioEventLoop.run')
f(6,0,3,1,'com/alibaba/arthas/deps/io/netty/channel/nio/NioEventLoop.processSelectedKeys')
f(7,0,3,1,'com/alibaba/arthas/deps/io/netty/channel/nio/NioEventLoop.processSelectedKeysOptimized')
f(8,0,3,1,'com/alibaba/arthas/deps/io/netty/channel/nio/NioEventLoop.processSelectedKey')
f(9,0,3,1,'com/alibaba/arthas/deps/io/netty/channel/nio/AbstractNioByteChannel$NioByteUnsafe.read')
f(10,0,2,1,'com/alibaba/arthas/deps/io/netty/channel/DefaultChannelPipeline.fireChannelRead')
f(11,0,2,1,'com/alibaba/arthas/deps/io/netty/channel/AbstractChannelHandlerContext.invokeChannelRead')
f(12,0,2,1,'com/alibaba/arthas/deps/io/netty/channel/AbstractChannelHandlerContext.invokeChannelRead')
f(13,0,2,1,'com/alibaba/arthas/deps/io/netty/channel/DefaultChannelPipeline$HeadContext.channelRead')
f(14,0,2,1,'com/alibaba/arthas/deps/io/netty/channel/AbstractChannelHandlerContext.fireChannelRead')
f(15,0,2,1,'com/alibaba/arthas/deps/io/netty/channel/AbstractChannelHandlerContext.invokeChannelRead')
f(16,0,2,1,'com/alibaba/arthas/deps/io/netty/channel/AbstractChannelHandlerContext.invokeChannelRead')
f(17,0,2,1,'io/termd/core/telnet/netty/TelnetChannelHandler.channelRead')
f(18,0,2,1,'io/termd/core/telnet/TelnetConnection.receive')
f(19,0,2,1,'io/termd/core/telnet/TelnetConnection.flushDataIfNecessary')
f(20,0,2,1,'io/termd/core/telnet/TelnetConnection.flushData')
f(21,0,2,0,'io/termd/core/telnet/TelnetTtyConnection.onData',0,0,1)
f(22,1,1,1,'io/termd/core/io/BinaryDecoder.write')
f(23,1,1,1,'io/termd/core/io/BinaryDecoder.write')
f(24,1,1,1,'io/termd/core/tty/ReadBuffer.accept')
f(25,1,1,1,'io/termd/core/tty/ReadBuffer.accept')
f(26,1,1,1,'io/termd/core/tty/TtyEventDecoder.accept')
f(27,1,1,1,'io/termd/core/tty/TtyEventDecoder.accept')
f(28,1,1,1,'io/termd/core/readline/Readline$Interaction$2.accept')
f(29,1,1,1,'io/termd/core/readline/Readline$Interaction$2.accept')
f(30,1,1,1,'io/termd/core/readline/Readline.access$500')
f(31,1,1,1,'io/termd/core/readline/Readline.deliver')
f(32,1,1,1,'io/termd/core/readline/Readline$Interaction.access$200')
f(33,1,1,1,'io/termd/core/readline/Readline$Interaction.handle')
f(34,1,1,1,'io/termd/core/readline/functions/BackwardDeleteChar.apply')
f(35,1,1,1,'io/termd/core/readline/LineBuffer.copy')
f(36,1,1,1,'io/termd/core/readline/LineBuffer.<init>')
f(37,1,1,1,'java/lang/Object.clone')
f(38,1,1,3,'JVM_Clone')
f(39,1,1,4,'CollectedHeap::common_mem_allocate_init(KlassHandle, unsigned long, Thread*)')
f(10,2,1,1,'com/alibaba/arthas/deps/io/netty/channel/DefaultMaxMessagesRecvByteBufAllocator$MaxMessageHandle.allocate')
f(11,2,1,1,'com/alibaba/arthas/deps/io/netty/buffer/AbstractByteBufAllocator.ioBuffer')
f(12,2,1,1,'com/alibaba/arthas/deps/io/netty/buffer/AbstractByteBufAllocator.directBuffer')
f(13,2,1,1,'com/alibaba/arthas/deps/io/netty/buffer/AbstractByteBufAllocator.directBuffer')
f(14,2,1,1,'com/alibaba/arthas/deps/io/netty/buffer/PooledByteBufAllocator.newDirectBuffer')
f(15,2,1,0,'com/alibaba/arthas/deps/io/netty/buffer/AbstractByteBufAllocator.toLeakAwareBuffer',0,0,1)
f(16,2,1,4,'InterpreterRuntime::_new(JavaThread*, ConstantPool*, int)')
f(17,2,1,4,'InstanceKlass::initialize(Thread*)')
f(18,2,1,4,'InstanceKlass::initialize_impl(instanceKlassHandle, Thread*)')
f(19,2,1,4,'InstanceKlass::link_class_impl(instanceKlassHandle, bool, Thread*)')
f(20,2,1,4,'InstanceKlass::link_class_impl(instanceKlassHandle, bool, Thread*)')
f(21,2,1,4,'Verifier::verify(instanceKlassHandle, Verifier::Mode, bool, Thread*)')
f(22,2,1,4,'ClassVerifier::verify_class(Thread*)')
f(6,3,1,1,'com/alibaba/arthas/deps/io/netty/channel/nio/NioEventLoop.select')
f(7,3,1,1,'com/alibaba/arthas/deps/io/netty/channel/nio/SelectedSelectionKeySetSelector.select')
f(8,3,1,1,'sun/nio/ch/SelectorImpl.select')
f(9,3,1,1,'sun/nio/ch/SelectorImpl.select')
f(10,3,1,1,'sun/nio/ch/SelectorImpl.lockAndDoSelect')
f(11,3,1,1,'sun/nio/ch/EPollSelectorImpl.doSelect')
f(12,3,1,1,'sun/nio/ch/EPollSelectorImpl.updateSelectedKeys')
f(13,3,1,0,'sun/nio/ch/SelectionKeyImpl.nioReadyOps',0,0,1)
f(14,3,1,4,'InterpreterRuntime::frequency_counter_overflow(JavaThread*, unsigned char*)')
f(15,3,1,4,'InterpreterRuntime::frequency_counter_overflow_inner(JavaThread*, unsigned char*)')
f(16,3,1,4,'SimpleThresholdPolicy::event(methodHandle, methodHandle, int, int, CompLevel, nmethod*, JavaThread*)')
f(17,3,1,4,'AdvancedThresholdPolicy::method_invocation_event(methodHandle, methodHandle, CompLevel, nmethod*, JavaThread*)')
f(18,3,1,4,'CompilationPolicy::can_be_compiled(methodHandle, int)')
f(2,4,1,1,'java/util/concurrent/ThreadPoolExecutor$Worker.run')
f(3,4,1,1,'java/util/concurrent/ThreadPoolExecutor.runWorker')
f(4,4,1,1,'java/util/concurrent/ScheduledThreadPoolExecutor$ScheduledFutureTask.run')
f(5,4,1,1,'java/util/concurrent/ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201')
f(6,4,1,1,'java/util/concurrent/FutureTask.run')
f(7,4,1,1,'java/util/concurrent/Executors$RunnableAdapter.call')
f(8,4,1,1,'com/taobao/arthas/core/shell/system/impl/ProcessImpl$CommandProcessTask.run')
f(9,4,1,1,'com/taobao/arthas/core/shell/command/impl/AnnotatedCommandImpl$ProcessHandler.handle')
f(10,4,1,1,'com/taobao/arthas/core/shell/command/impl/AnnotatedCommandImpl$ProcessHandler.handle')
f(11,4,1,1,'com/taobao/arthas/core/shell/command/impl/AnnotatedCommandImpl.access$100')
f(12,4,1,1,'com/taobao/arthas/core/shell/command/impl/AnnotatedCommandImpl.process')
f(13,4,1,1,'com/taobao/arthas/core/util/UserStatUtil.arthasUsageSuccess')
f(14,4,1,1,'com/taobao/arthas/core/util/UserStatUtil.arthasUsage')
f(15,4,1,0,'com/taobao/arthas/core/util/UserStatUtil$RemoteJob.appendQueryData',0,0,1)
f(1,5,1,3,'msort_with_tmp.part.0')
f(2,5,1,4,'LinearScan::interval_cmp(Interval**, Interval**)')
f(1,6,2,3,'start_thread')
f(2,6,2,3,'java_start(Thread*)')
f(3,6,1,4,'JavaThread::run()')
f(4,6,1,4,'JavaThread::thread_main_inner()')
f(5,6,1,4,'CompileBroker::compiler_thread_loop()')
f(6,6,1,4,'CompileBroker::invoke_compiler_on_method(CompileTask*)')
f(7,6,1,4,'Compiler::compile_method(ciEnv*, ciMethod*, int)')
f(8,6,1,4,'Compilation::Compilation(AbstractCompiler*, ciEnv*, ciMethod*, int, BufferBlob*)')
f(9,6,1,4,'Compilation::compile_method()')
f(10,6,1,4,'Compilation::compile_java_method()')
f(11,6,1,4,'Compilation::build_hir()')
f(12,6,1,4,'IR::IR(Compilation*, ciMethod*, int)')
f(13,6,1,4,'IRScope::IRScope(Compilation*, IRScope*, int, ciMethod*, int, bool)')
f(14,6,1,4,'GraphBuilder::GraphBuilder(Compilation*, IRScope*)')
f(15,6,1,4,'GraphBuilder::iterate_all_blocks(bool)')
f(16,6,1,4,'GraphBuilder::iterate_bytecodes_for_block(int)')
f(17,6,1,4,'GraphBuilder::invoke(Bytecodes::Code)')
f(18,6,1,4,'GraphBuilder::try_inline(ciMethod*, bool, Bytecodes::Code, Instruction*)')
f(19,6,1,4,'GraphBuilder::try_inline_full(ciMethod*, bool, Bytecodes::Code, Instruction*)')
f(20,6,1,4,'GraphBuilder::iterate_all_blocks(bool)')
f(21,6,1,4,'GraphBuilder::iterate_bytecodes_for_block(int)')
f(3,7,1,4,'WatcherThread::run()')
f(4,7,1,4,'WatcherThread::sleep() const')
f(5,7,1,4,'Monitor::wait(bool, long, bool)')
f(6,7,1,4,'Monitor::IWait(Thread*, long)')
f(7,7,1,4,'os::PlatformEvent::park(long)')

search();
</script></body></html>
